package com.zhujy.hash;

import java.util.HashMap;
import java.util.Map;

public class TwoSum {
	public int[] twoSum(int[] nums, int target) {
		int res[] = {0,0};
		Map<Integer, Integer> map = new HashMap<Integer,Integer>();
		int other = target;
		for(int i=0,len=nums.length;i<len;i++) {
			other = target - nums[i];
			if(map.containsKey(other)) {
				res[1] = i;
				res[0] = map.get(other);
				return res;
			}
			map.put(nums[i], i);
		}
        return null;
    }
	
	public static void main(String[] args) {
		int nums[] = {2, 7, 11, 15}, target = 9;
		TwoSum test = new TwoSum();
		int res[] = test.twoSum(nums, target);
		for(int i=0;i<res.length;i++) {
			System.out.println(res[i]);
		}
	}
}
